﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FurnitureSystem
{
    
    public partial class frmQuanLyLoaiSP : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlDataReader dr;
        private int ID;
        public frmQuanLyLoaiSP()
        {
            InitializeComponent();
            
        }
        public void LoadLoaiSP()
        {
            conn = connect.Getconect();
            conn.Open();
            string sql = "select * from Type";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "type");
            
            lstLoaiSP.DataSource = ds.Tables[0];
            lstLoaiSP.DisplayMember = "Typeinfo";
            lstLoaiSP.ValueMember = "Typeid";
            conn.Close();



        }

        private void frmCapNhatLoaiSP_Load(object sender, EventArgs e)
        {
            if (frmLogin.depart == "admin     ") {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
            } 
            this.btnCapNhat.Enabled = false;
            LoadLoaiSP();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (txtLoai.Text == "")
            {
                MessageBox.Show("Vui lòng nhập thông tin trước khi cập nhật");
            }
            else
            {
                ID = Convert.ToInt32(lstLoaiSP.SelectedValue);
                conn = connect.Getconect();
                conn.Open();
                string sql = "update Type set Typeinfo = '" + txtLoai.Text + "' where Typeid = '" + ID + "'";
                cmd = new SqlCommand(sql, conn);
                dr = cmd.ExecuteReader();
                this.btnCapNhat.Enabled = false;
                this.btnSua.Enabled = true;
                txtLoai.Text = "";
               
                conn.Close();
                LoadLoaiSP();
               
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            conn = connect.Getconect();
            conn.Open();
            
            string sql = "insert into Type(Typeinfo) values ('" + txtLoai.Text + "')";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            dr = cmd.ExecuteReader();
            conn.Close();
            txtLoai.Text = "";
            LoadLoaiSP();

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            ID = Convert.ToInt32(lstLoaiSP.SelectedValue);
            conn = connect.Getconect();
            conn.Open();
            string sql = "delete from Type where Typeid = '" + ID + "'";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            dr = cmd.ExecuteReader();
            conn.Close();
            txtLoai.Text = "";
            LoadLoaiSP();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            
            this.Hide();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
            conn = connect.Getconect();
            conn.Open();
            ID = Convert.ToInt32(lstLoaiSP.SelectedValue);
           
            string sql = "select Typeinfo from Type where Typeid='" + ID + "'";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            txtLoai.Text = (string)cmd.ExecuteScalar();
            
            dr = cmd.ExecuteReader();
            txtLoai.Select();
            conn.Close();
            this.btnSua.Enabled = false;
            this.btnCapNhat.Enabled = true;
            
            
        }

        private void btnTrove_Click(object sender, EventArgs e)
        {
            frmMain frmMain = new frmMain();
            frmMain.Show();
            this.Hide();

        }

        
    }
}
